package com.haizhi.workstation.listener;

import com.haizhi.workstation.mq.MQSender;
import com.haizhi.workstation.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;

/** 
 * 自定义文件监听器  监听指定文件夹下文件
 * @author  WangLinbao
 * */
//@Component
@Slf4j
public class MyFileListener extends FileAlterationListenerAdaptor{

    @Autowired
    private MQSender sender;

    public MyFileListener(MQSender sender){
        this.sender = sender;
    }
    /**
     * 对创建文件事件的监听
     * @param file 新增的文件
     */
    @Override
    public void onFileCreate(File file) {
        //读取文件,并解析
        String JsonContext = new JSONUtil().getDataFromFile(file);
        sender.sendFileMessage(JsonContext,file.getName());
//        //public的receive文件备份
//        JSONUtil.saveDataToFile(Constants.PUBLIC_RECEIVE_FOLDER_BACKUPS+file.getName(),JsonContext);
//        JSONArray jsonArray = JSONArray.fromObject(JsonContext);
//        int size = jsonArray.size();
//        log.info("Size: " + size);
//        for(int  i = 0; i < size; i++){
//            JSONObject jsonObject = jsonArray.getJSONObject(i);
//            try {
//                String keyContent = RSAUtil.decryptByPrivateKey(jsonObject.get("key")+"", Constants.PRIVATE_KEY);//解密
//                log.info("加密文件的文件头====={}",keyContent);
//                com.alibaba.fastjson.JSONObject jsonObject1 = JSON.parseObject(keyContent);
//                String requestUrl = (String)jsonObject1.get("requestUrl");
//                String responseUrl = (String)jsonObject1.get("responseUrl");
//
//                //String appId = (String)jsonObject1.get("appId");
//
//                String params = (String)jsonObject1.get("params");
//                String data = jsonObject.get("data") + "";
//                //回调时传的参数
//                String requestParam = "";
//                if(!StringUtils.hasLength(responseUrl) || !StringUtils.hasLength(requestUrl)){
//                    log.info("requestUrl Or responseUrl is Empty");
//                    log.info("requestUrl====={}",requestUrl);
////                    throw new GlobalException(CodeMsg.ERR_OPERATION);
//                }
//                requestParam = requestParam+"responseUrl="+responseUrl;
//                /*if(!StringUtils.hasLength(params) *//*|| !StringUtils.hasLength(appId)*//*){
//                    throw new GlobalException(CodeMsg.ERR_OPERATION);
//                }*/
//                requestParam = requestParam+"&params="+params;
//                if(StringUtils.hasLength(data)){
//                    //将data数据作为参数拼接到参数中
//                    requestParam = requestParam+"&data="+data;
//                    log.info("requestParam: {}",requestParam);
//                }
//                //解析出responseUrl并回调
//                log.info("responseUrl===== {}",responseUrl);
//                log.info("requestUrl===== {}",requestUrl);
//                log.info("requestParam===== {}",requestParam);
//
//
//                //解析receive文件requestUrl，去访问
//                //System.out.println(responseUrl);
//            } catch (GlobalException e) {
//                throw e;
//            }catch (Exception e) {
//                log.error("错误文件："+Constants.PUBLIC_RECEIVE_FOLDER_BACKUPS+file.getName());
//                e.printStackTrace();
//            }
//            System.out.println("[" + i + "]data=" + jsonObject.get("data"));
//        }
//
//        System.out.println("[新建]:" + file.getAbsolutePath());
    }  
    @Override  
    public void onFileChange(File file) {  
        System.out.println("[修改]:" + file.getAbsolutePath());  
    }  
    @Override  
    public void onFileDelete(File file) {  
        System.out.println("[删除]:" + file.getAbsolutePath());  
    }  
}